package com.quan.common.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.quan.common.base.constant.CommonConstant;

/**
 * RabbitMQ配置<br>
 * 1：创建交换机
 * 2：创建队列
 * 3：将队列与交换机绑定
 * @author yhaoquan
 *
 */
@Configuration
public class RabbitMQConfiguration {
	
	/**
	 * 交换机类型Topic Exchange
	 * 广播模式或者订阅模式
	 * @return
	 */
	@Bean
	public TopicExchange exchangeTopic() {
		return new TopicExchange(CommonConstant.EXCHANGE_TOPIC) ;
	}
	
	/**
	 * 交换机类型Fanout Exchange
	 * 广播模式或者订阅模式
	 * @return
	 */
	@Bean
	public FanoutExchange exchangeFanout() {
        return new FanoutExchange(CommonConstant.EXCHANGE_FANOUT) ;
    }
	
	
	/**
	 * RabbitMQ Topic队列消息
	 * @return
	 */
	@Bean
	public Queue defaultTopicQueue() {
		return new Queue(CommonConstant.MQ_TOPIC_MESSAGE_DEFAULT) ;
	}
	@Bean
	public Queue mailTopicQueue() {
		return new Queue(CommonConstant.MQ_TOPIC_MESSAGE_MAIL) ;
	}
	@Bean
	public Queue loggerTopicQueue() {
		return new Queue(CommonConstant.MQ_TOPIC_MESSAGE_LOGGER) ;
	}
	@Bean
	public Queue ossUploadTopicQueue() {
		return new Queue(CommonConstant.MQ_TOPIC_MESSAGE_OSS_UPLOAD) ;
	}
	@Bean
	public Queue ossDeleteTopicQueue() {
		return new Queue(CommonConstant.MQ_TOPIC_MESSAGE_OSS_DELETE) ;
	}
	
	
	/**
	 * RabbitMQ Fanout队列消息
	 * @return
	 */
	@Bean
	public Queue defaultFanoutQueue() {
		return new Queue(CommonConstant.MQ_FANOUT_MESSAGE_DEFAULT) ;
	}
	
	
	
	/**
	 * 将（队列）与（TopicExchange）交换机绑定
	 * @param defaultTopicQueue
	 * @param exchangeTopic
	 * @return
	 */
	@Bean
	public Binding bindingTopicExchangeMessageDefault(Queue defaultTopicQueue, TopicExchange exchangeTopic) {
		return BindingBuilder.bind(defaultTopicQueue).to(exchangeTopic).with(CommonConstant.MQ_TOPIC_MESSAGE_DEFAULT);
	}
	
	@Bean
	public Binding bindingTopicExchangeMessageMail(Queue mailTopicQueue, TopicExchange exchangeTopic) {
		return BindingBuilder.bind(mailTopicQueue).to(exchangeTopic).with(CommonConstant.MQ_TOPIC_MESSAGE_MAIL);
	}
	
	@Bean
	public Binding bindingTopicExchangeMessageLogger(Queue loggerTopicQueue, TopicExchange exchangeTopic) {
		return BindingBuilder.bind(loggerTopicQueue).to(exchangeTopic).with(CommonConstant.MQ_TOPIC_MESSAGE_LOGGER);
	}
	
	@Bean
	public Binding bindingTopicExchangeMessageOssUpload(Queue ossUploadTopicQueue, TopicExchange exchangeTopic) {
		return BindingBuilder.bind(ossUploadTopicQueue).to(exchangeTopic).with(CommonConstant.MQ_TOPIC_MESSAGE_OSS_UPLOAD);
	}
	@Bean
	public Binding bindingTopicExchangeMessageOssDelete(Queue ossDeleteTopicQueue, TopicExchange exchangeTopic) {
		return BindingBuilder.bind(ossDeleteTopicQueue).to(exchangeTopic).with(CommonConstant.MQ_TOPIC_MESSAGE_OSS_DELETE);
	}
	
	/**
	 * 将（队列）与（FanoutExchange）交换机绑定
	 * @param defaultFanoutQueue
	 * @param exchangeFanout
	 * @return
	 */
	@Bean
	public Binding bindingFanoutExchangeMessage(Queue defaultFanoutQueue, FanoutExchange exchangeFanout) {
        return BindingBuilder.bind(defaultFanoutQueue).to(exchangeFanout);
    }
	
	
}
